package hong.jun.service.impl;



import dev.morphia.dao.BasicDAO;
import dev.morphia.query.Query;
import dev.morphia.query.UpdateOperations;
import hong.jun.service.IMongoBaseService;

import java.util.List;


/**
 * @Description:
 * @Author: yanghongjun
 * @Date: 2023/11/20
 **/
public abstract class MongoBaseServiceImpl<T, K> implements IMongoBaseService<T,K> {

    public abstract BasicDAO getDao() ;

    public abstract K getKeyName() ;

    @Override
    public T selectOneData(K id) {
        BasicDAO dao = getDao();
        if (dao == null){
            return null;
        }
        return (T) dao.get(id);
    }

    @Override
    public List<T> selectAllDatas() {
        BasicDAO dao = getDao();
        if (dao == null){
            return null;
        }
        List<T> list = dao.createQuery().asList();
        return list;
    }

    @Override
    public void addData(T t) {
        BasicDAO dao = getDao();
        if (dao == null){
            return;
        }
        dao.save(t);
    }

    @Override
    public boolean delOneData(T t) {
        BasicDAO dao = getDao();
        if (dao == null){
            return false;
        }
        dao.delete(t);
        return true;
    }

    @Override
    public long getCount() {
        BasicDAO dao = getDao();
        if (dao == null){
            return 0;
        }
        return dao.count();
    }

    @Override
    public boolean updateOneData(T t) {
        BasicDAO dao = getDao();
        if (dao == null){
            return false;
        }
        dao.save(t);
        return true;
    }

    @Override
    public boolean updateOneFiledData(K k, String filed, Object object) {
        return false;
    }
}
